package com.ellricky.tv.ui

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ActivityUtil
import com.ellricky.baselib.adapter.CommonGridItemDecoration
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.entity.IdNameEntity
import com.ellricky.baselib.entity.course.FileSearchEntity
import com.ellricky.baselib.entity.new.CourseStage
import com.ellricky.baselib.http.Api
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEListCallback
import com.ellricky.tv.Const
import com.ellricky.tv.R
import com.ellricky.tv.adapter.TvMultiAdapter
import com.ellricky.tv.util.TvECallback
import kotlinx.android.synthetic.main.tv_activity_age_select.*
import kotlinx.android.synthetic.main.tv_base_layout_header.*
import java.util.HashMap

class TvAgeSelectActivity : BaseTvActivity<Void, BasePresenter<Void>>() {

    private val mAgeAdapter by lazy {
        TvMultiAdapter()
    }

    private var mClassTypeId: String? = null
    private var mClassTypeName: String? = null

    override fun getContentLayout(): Int {
        return R.layout.tv_activity_age_select
    }

    override fun initUIView() {
        ActivityUtil.addActivity(this)
        initRv()
    }

    override fun initOnclick() {
        tvBack.setOnClickListener(this)
        tvLogout.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.tvBack -> finish()
            R.id.tvLogout -> logout()
        }
    }

    override fun parseParams() {
        intent.extras?.also {

            mClassTypeId = it.getString("course_type_id")
            mClassTypeName = it.getString("course_name")
            tvCourseName.text = "$mClassTypeName - 阶段选择"

            when (mClassTypeName) {
                "美学基础课" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_1)
                "艺术创造" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_3)
                "体验课" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_5)
                "艺术史" -> ivCourseType?.setBackgroundResource(R.drawable.tv_icon_course_2)
            }
        }
    }

    private fun initRv() {
        mAgeAdapter.also {
            rvAge.layoutManager = GridLayoutManager(this, 6)
            rvAge.adapter = it
            rvAge.addItemDecoration(CommonGridItemDecoration(this, 6, 10, true))

            it.setOnItemClickListener { parent,
                                        view,
                                        position,
                                        id ->

                val data = it.dataSource[position]
                val bundle = Bundle()
                bundle.putString("stage_id", data.stage_id)
                bundle.putString("name", mClassTypeName ?: "")
                bundle.putString("classTypeId", mClassTypeId ?: "")
                EAppUtil.jumpPageWithParam(this, TvCourseSelectActivity::class.java, bundle)
            }
        }
    }

    override fun fetchData() {
        val url = EAppUtil.getETvApiHost()
        val paramMap: MutableMap<String, String> = HashMap()
        paramMap["action"] = "teacherCoursestageList"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["course_id"] = mClassTypeId?:""

        HttpUtils.getInstance()
            .postFrom(url, paramMap, object : NewEListCallback<CourseStage>(this) {
                override fun onSuccess(data: MutableList<CourseStage>, msg: String?) {
                    if (data.isNotEmpty()) {
                        mAgeAdapter.refresh(data)
                    }
                }

                override fun onErrorCode(code: String?, msg: String?) {
                    super.onErrorCode(code, msg)
                    mAgeAdapter.refresh(arrayListOf())
                }
            })
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityUtil.removeActivity(this)
        HttpUtils.getInstance().cancelTag(this)
    }
}